<?php
class C_Document extends Controller {
	var $template_mod;
	var $_db;
	var $join_db;
	var $limit = 50;
	
	function C_Document($template_mod = "general") {
		parent::Controller();
		if (Celini::getCurrentController() == 'Document') {
			$menu =& Menu::getInstance();
			$menu->currentSection = 'document';
		}
		$this->template_mod = $template_mod;
		$this->view->path = 'document';
	}		
	
	public function actionUploadDocument(){			
		$thumnails = $this->getDocuments($_GET['recordId']);
		$_SESSION['current']['recordId'] = $_GET['recordId'];
		$this->assign('all_thumnails', $thumnails);
		echo $this->view->render("upload.html");
	}
	
	public function actionViewDocument(){
		$thumnails = $this->getDocuments($_GET['recordId']);
		$_SESSION['current']['recordId'] = $_GET['recordId'];
		if (!empty($thumnails)) {
			$this->assign('all_thumnails',$thumnails);
			echo $this->view->render("view.html");
		} else {
			//@localization
			echo "<div class='form_container' style='width: 220px'><span class='label'>".L10nHelper::_("There is no image info")."</span></div>";
		}
	}
	
	public function actionUploadDocumentAjax(){		
		// It would be nice if document_store_id is passed instead of medical_record_id
		$msg = '';
		$ext = explode("/", $_FILES['file']['type']);
		$ext = strtolower($ext[count($ext)-1]);
		if(!empty($_FILES['file']['error'])) {
			switch($_FILES['file']['error']) {
				case '1':
					$msg = L10nHelper::_("File size is over the limitation");
					break;
				case '2':
					$msg = L10nHelper::_("File size is not within HTML file size limitation");
					break;
				case '3':
					$msg = L10nHelper::_("Only uploaded a part of file");
					break;
				case '4':
					$msg = L10nHelper::_("Could not upload");
					break;	
				case '6':
					$msg = L10nHelper::_("There is not a temporary folder for file yet");
					break;
				case '7':
					$msg = L10nHelper::_("The file could not write");
					break;
				case '8':
					$msg = L10nHelper::_("Loading file is interrupted");
					break;
				case '999':
				default:
					$msg = L10nHelper::_("There is an error in loading file");
			}
		} elseif ($_FILES['file']['type'] != "image/jpeg" && $_FILES['file']['type'] != "image/pjpeg" && $_FILES['file']['type'] != "image/gif" && $_FILES['file']['type'] != "image/png" && $_FILES['file']['type'] != "image/x-png"){
			//@localization
			$msg = L10nHelper::_("Image format, %s is not supported",$ext);
		} elseif (empty($_FILES['file']['tmp_name']) || $_FILES['file']['tmp_name'] == 'none') {
			//@localization
			$msg = L10nHelper::_("Can not upload...");
		} else {
		   	$uploadedDocument = Celini::newORDO('Document');		 
		   	$storeID = $uploadedDocument->getStoreID($_SESSION['current']['recordId']);
		   	$obj = Celini::newORDO('MedicalRecord');
		   	$patient = $obj->getMedicalRecord($_SESSION['current']['recordId']);
	   		$documentID = $uploadedDocument->addNewDocument($patient[0]['patient_id'], $storeID, $_FILES['file']);	
	   		// Return this document to client
	   		$newDoc = $uploadedDocument->getDocument($documentID); 
		}	
  		$newDoc['message'] = $msg; 		
   		echo json_encode($newDoc);
	}   		
	
	function actionSetDescription(){
		$docId = $_POST['docId'];
		$_POST['newDesc'] = Utils::sanitizeText($_POST['newDesc']);
		$currentDoc = Celini::newORDO('Document');
		$currentDoc->setName($_POST['newDesc'], $docId);
		echo $_POST['newDesc'];			
	}
	
	function actionRemoveDocument(){
		$doc = Celini::newORDO('Document');
		$doc->removeDocument($_GET['docId']);

	}
	
	private function getDocuments($recordId, $count=''){
		$record = Celini::newORDO('Document');
		$storeId = $record->getStoreID($recordId);
		$docs = $record->getDocuments($storeId, $count);
		return $docs;		
	}
}

?>